package de.dhbw.karlsruhe.picsim.model.decode;

import de.dhbw.karlsruhe.picsim.model.commands.PicInstruction;
import de.dhbw.karlsruhe.picsim.model.commands.impl.BTFSC;
import de.dhbw.karlsruhe.picsim.model.commands.impl.BTFSS;
import de.dhbw.karlsruhe.picsim.model.commands.impl.BcF;
import de.dhbw.karlsruhe.picsim.model.commands.impl.BsF;
import de.dhbw.karlsruhe.picsim.model.memory.OpCode;
import de.dhbw.karlsruhe.picsim.util.Bits;
import de.dhbw.karlsruhe.picsim.util.impl.BitUtilities;
import org.jetbrains.annotations.NotNull;

/**
 * Created by IntelliJ IDEA.
 * User: goesen
 * Date: May 5, 2010
 * Time: 12:13:45 PM
 */
final class BitDecoder implements Decoder {
    @NotNull
    @Override
    public PicInstruction decode(@NotNull OpCode opCode) {
        final Bits value = opCode.getValue();
        final Bits bitArgs = BitUtilities.subBits(value, 7, 7 + 3);
        final int bitArg = BitUtilities.fromLeastSignificantBitFirst(bitArgs);
        final Bits fileArg = BitUtilities.subBits(value, 0, 7);
        if (value.isSet(11) && value.isSet(10)) {
            return new BTFSS(fileArg, bitArg);
        } else if (!value.isSet(11) && value.isSet(10)) {
            return new BsF(fileArg, bitArg);
        } else if (value.isSet(11) && !value.isSet(10)) {
            return new BTFSC(fileArg, bitArg);
        } else if (!value.isSet(11) && !value.isSet(10)) {
            return new BcF(fileArg, bitArg);
        }
        throw new IllegalStateException();

    }
}
